package com.system.permissiondemo.controller;

import com.system.permissiondemo.beans.PageQuery;
import com.system.permissiondemo.beans.PageResult;
import com.system.permissiondemo.common.RequestHolder;
import com.system.permissiondemo.common.ResultDTO;
import com.system.permissiondemo.param.UserParam;
import com.system.permissiondemo.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/sys/user")
@Api(tags = "用户信息管理")
@Slf4j
public class UserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "username",value = "用户名", required = true, paramType = "query",dataType = "String",defaultValue = "joe"),
            @ApiImplicitParam(name = "telephone",value = "手机号", required = true, paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "mail",value = "邮箱", required = true, paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "remark",value = "备注", paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "deptId",value = "用户所在部门id",required = true,paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "status",value = "用户状态1，正常；0，冻结；2，删除",required = true,paramType = "query",dataType ="Integer",defaultValue = "1")
    })
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResultDTO saveUser(UserParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        sysUserService.save(param);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "更新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "username",value = "用户名", required = true, paramType = "query",dataType = "String",defaultValue = "joe"),
            @ApiImplicitParam(name = "telephone",value = "手机号", required = true, paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "mail",value = "邮箱", required = true, paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "remark",value = "备注", paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "deptId",value = "用户所在部门id",required = true,paramType = "query",dataType ="Integer"),
            @ApiImplicitParam(name = "status",value = "用户状态1，正常；0，冻结；2，删除",required = true,paramType = "query",dataType ="Integer",defaultValue = "1")
    })
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public ResultDTO updateUser(UserParam param){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        sysUserService.update(param);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/delete/{userId}",method = RequestMethod.DELETE)
    public ResultDTO deleteUser(@PathVariable("userId")Integer userId){
        if (RequestHolder.getCurrentUser()==null){
            return ResultDTO.errorOfLogin();
        }
        sysUserService.deleteById(userId);
        return ResultDTO.okOf();
    }

    @ApiOperation(value = "根据部门分页查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页码",required = true,paramType = "query",dataType ="int",defaultValue = "1"),
            @ApiImplicitParam(name = "deptId",value = "用户所在部门id",required = true,paramType = "query",dataType ="int"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示数量",required = true,paramType = "query",dataType ="int",defaultValue = "10")
    })
    @RequestMapping(value = "/page",method = RequestMethod.POST)
    public ResultDTO page(Integer pageNo,Integer pageSize,@RequestParam("deptId") Integer depaId){
        PageQuery page = new PageQuery();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        PageResult result = sysUserService.getPageByDeptId(depaId,page);
        result.setPageQuery(page);
        return ResultDTO.okOf(result);
    }
}
